package br.com.ordersys.framework.utils;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.TreeMap;

public class BeanUtils {

	public static Map<String, Object> buildMapFromGetters(Object o) {
		Map<String, Object> m = new TreeMap<String, Object>();

		try {

			Method methods[] = o.getClass().getDeclaredMethods();

			for (Method method : methods) {
				// metodos com zero argumentos e que retorne <> void
				if (method.getName().startsWith("get") && !method.getReturnType().equals(void.class) && method.getGenericParameterTypes().length == 0) {
					char[] vName = method.getName().substring(3).toCharArray();

					if (Character.isUpperCase(vName[0])) {
						vName[0] = Character.toLowerCase(vName[0]);
					}

					String property = new String(vName);
					m.put(property, method.invoke(o, null));
				}
			}

		} catch (Exception e) {
			ExceptionUtils.throwAsUnchecked(e);
		}
		return m;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
